package framework

import (
	"encoding/json"
	"net/http"
)

//Result ...
type Result struct {
	code    int    `json:"code"`
	message string `json:"message"`
	data    string `json:"data"`
}

func (r *Result) toJSON() ([]byte, error) {
	return json.Marshal(r)
}

//ResultJSON ...
func ResultJSON(w http.ResponseWriter, data Result) {
	w.Header().Set("Content-Type", "application/json")
	json, _ := json.Marshal(data)
	w.Write(json)
}

//NewResult ...
func NewResult(code int, message string, data interface{}) Result {
	json, _ := json.Marshal(data)
	result := Result{
		code:    code,
		message: message,
		data:    string(json),
	}
	return result
}
